package uwosh.cs342.project1;

import java.util.ArrayList;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ContactsDataSource {

	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;

	public ContactsDataSource(Context context) {
		dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
		//dbHelper.onUpgrade(database, 1, 1);
	}

	public void close() {
		dbHelper.close();
	}

	public Contact createContact(Contact co,String group) {
		ContentValues values = new ContentValues();

		values.put("contactFirstName", co.getContactFirstName());
		values.put("contactMiddleName", co.getContactMiddleName());
		values.put("contactLastName", co.getContactLastName());
		values.put("contactCellPhone", co.getContactCellPhone());
		values.put("contactHomePhone", co.getContactHomePhone());
		values.put("contactAltPhone", co.getContactAltPhone());
		values.put("contactAddress", co.getContactAddress());
		values.put("contactEmail", co.getContactEmail());
		values.put("contactBirthday", co.getContactBirthday());
		values.put("contactComments", co.getContactComments());
		values.put("contactGroup", group);

		long insertId = database.insert(MySQLiteHelper.TABLE_C, null, values);

		co.setID(insertId);
		System.out.println(insertId);

		/* To show how to query
		Cursor cursor = database.query("\""+MySQLiteHelper.TABLE_C+"\"", null,
				MySQLiteHelper.C_ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		return cursorToContact(cursor);*/
		return co;
	}

	public void editContact(Contact co,String group) {
		ContentValues values = new ContentValues();

		values.put("contactFirstName", co.getContactFirstName());
		values.put("contactMiddleName", co.getContactMiddleName());
		values.put("contactLastName", co.getContactLastName());
		values.put("contactCellPhone", co.getContactCellPhone());
		values.put("contactHomePhone", co.getContactHomePhone());
		values.put("contactAltPhone", co.getContactAltPhone());
		values.put("contactAddress", co.getContactAddress());
		values.put("contactEmail", co.getContactEmail());
		values.put("contactBirthday", co.getContactBirthday());
		values.put("contactComments", co.getContactComments());
		values.put("contactGroup", group);
		
		database.update(MySQLiteHelper.TABLE_C, values, MySQLiteHelper.C_ID+" = "+co.getID(), null);
	}

	public long createGroup(String name) {
		ContentValues values = new ContentValues();

		values.put("name", name);

		long insertId = database.insert("\""+MySQLiteHelper.TABLE_G+"\"", null, values);

		return insertId;
	}

	public void deleteContact(Contact contact) {
		String id = "" + contact.getID();
		System.out.println("Contact deleted with id: " + id);
		database.delete(MySQLiteHelper.TABLE_C, MySQLiteHelper.C_ID + " = "
				+ id, null);
	}
	
	public void deleteGroup(String groupName) {
		System.out.println("Contact deleted with id: " + groupName);
		database.delete(MySQLiteHelper.TABLE_G, MySQLiteHelper.G_NAME + " = \"" + groupName+"\"", null);
		database.delete(MySQLiteHelper.TABLE_C, MySQLiteHelper.C_GROUP + " = \"" + groupName+"\"", null);
		
	}
	
	public ArrayList<String> getAllGroups() {
		ArrayList<String> contacts = new ArrayList<String>();
		Cursor cursor = database.query("\""+MySQLiteHelper.TABLE_G+"\"", null,
				null, null, null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			contacts.add(cursor.getString(1));
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return contacts;
	}

	public ArrayList<Contact> getAllContacts(String groupName) {
		String group = "\""+MySQLiteHelper.TABLE_C+"\".\""+MySQLiteHelper.C_GROUP+"\"";
		ArrayList<Contact> contacts = new ArrayList<Contact>();
		Cursor cursor = database.query("\""+MySQLiteHelper.TABLE_C+"\"", null,
				group +" = " + "\""+groupName+"\"", null, null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Contact contact = cursorToContact(cursor);
			contacts.add(contact);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return contacts;
	}

	private Contact cursorToContact(Cursor cursor) {
		Contact contact = new Contact();
		contact.setID(cursor.getLong(0));
		contact.setContactFirstName(cursor.getString(1));
		contact.setContactMiddleName(cursor.getString(2));
		contact.setContactLastName(cursor.getString(3));
		contact.setContactCellPhone(cursor.getString(4));
		contact.setContactHomePhone(cursor.getString(5));
		contact.setContactAltPhone(cursor.getString(6));
		contact.setContactAddress(cursor.getString(7));
		contact.setContactEmail(cursor.getString(8));
		contact.setContactComments(cursor.getString(9));
		contact.setContactBirthday(cursor.getString(10));
		return contact;
	}
}